<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/28 15:39
 * Desc: 系统模块
 */
class Controller_Pc_Api_V2_System extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_System
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_System
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('system', $this->request_params);

        // 服务
        $this->service = new Service_Api_System();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 应用与模块
    public function action_app()
    {
        try {
            $this->validation->app()->check();

            $cache_key = Util_CacheKey::api_v2('system_app', Util_CacheKey::make_arguments($this->request_params));
            $data      = Model_Api_V2_Common::cache('get', $cache_key);
            if (!$data) {
                $data        = $this->service->app($this->request_params);
                $expire_time = rand(30, 60);
                Model_Api_V2_Common::cache('set', $cache_key, $data, $expire_time);
            }

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 用户协议
    public function action_agreement()
    {
        try {
            $this->validation->agreement()->check();

            $data = $this->service->agreement($this->request_params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 后台配置参数
    public function action_get_sys_configs()
    {
        try {
            $this->validation->get_sys_configs()->check();

            $data = $this->service->sys_configs($this->request_params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // uniApp新广告位
    public function action_new_ads()
    {
        try {
            $this->validation->new_ads()->check();

            $data = $this->service->new_ads($this->request_params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取uniapp页面模板编号
    public function action_get_template_no()
    {
        try {
            $data = $this->service->template_no($this->request_params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取系统汇率
    public function action_exchange_rate()
    {
        try {
            $cache_key = Util_CacheKey::api_v2('system_exchange_rate');
            $data      = Model_Api_V2_Common::cache('get', $cache_key);
            if (!$data) {
                $data        = $this->service->exchange_rate();
                $expire_time = rand(30, 60);
                Model_Api_V2_Common::cache('set', $cache_key, $data, $expire_time);
            }
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    public function action_consulting()
    {
        try {
            $this->validation->consulting()->check();
            $data = $this->service->consulting($this->request_params);
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


}
